﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ImobitechModel.com.Controller;

namespace Imobitech.Gestao
{
    public partial class listaReparoConserto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = Request.QueryString["id"] != null ? int.Parse(Request.QueryString["id"].ToString()) : 0;
                ControladorGestao cg = new ControladorGestao();
                gdvSrvicos.DataSource = cg.buscarReparoConserto();
                gdvSrvicos.DataBind();
            }
        }

        protected void gdvSrvicos_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image img = (Image)e.Row.FindControl("imageAprovado");
                if (e.Row.Cells[6].Text == "True")
                {
                    img.ImageUrl = "../imagens/ok.jpg";
                    e.Row.Cells[6].Text = "Aprovado";
                    //e.Row.Cells[7].Visible = true;
                    (e.Row.Cells[e.Row.Cells.Count - 3].Controls[0] as Button).Visible = false;
                }
                else
                {
                    img.ImageUrl = "../imagens/excluir.jpg";
                    e.Row.Cells[6].Text = "Aguardando Aprovação";
                    //e.Row.Cells[8].Visible = false;
                    (e.Row.Cells[e.Row.Cells.Count - 3].Controls[0] as Button).Visible = true;
                }
                //e.Row.Cells[3].Visible = false;
                Image img2 = (Image)e.Row.FindControl("imageRealizado");
                if (e.Row.Cells[8].Text == "True")
                {
                    img2.ImageUrl = "../imagens/ok.jpg";
                    e.Row.Cells[8].Text = "Realizado";
                    (e.Row.Cells[e.Row.Cells.Count - 1].Controls[0] as Button).Visible = true;
                }
                else
                {
                    img2.ImageUrl = "../imagens/excluir.jpg";
                    e.Row.Cells[8].Text = "Ainda não foi realizado";
                    (e.Row.Cells[e.Row.Cells.Count - 1].Controls[0] as Button).Visible = false;
                }
                // e.Row.Cells[5].Visible = false;
            }
        }

        protected void gdvSrvicos_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ControladorGestao cg = new ControladorGestao();
            if (e.CommandName.CompareTo("Aprovar") == 0)
            {
                int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
                TextBox txtValor = (TextBox)gdvSrvicos.Rows[currentRowIndex].FindControl("txtValor");
                cg.acaoAprovar(int.Parse(gdvSrvicos.DataKeys[currentRowIndex].Value.ToString()), decimal.Parse(txtValor.Text));
            }
            else if (e.CommandName.CompareTo("Realizado") == 0)
            {
                int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
                cg.baixaBoleto(int.Parse(gdvSrvicos.DataKeys[currentRowIndex].Value.ToString()));
            }
        }
    }
}